//
//  AppDelegate.m
//  PaiQuan
//
//  Created by Mac on 16/7/4.
//  Copyright © 2016年 huigu. All rights reserved.
//

#import "AppDelegate.h"
#import "PQTabBarController.h"
#import <RongIMKit/RongIMKit.h>
#import <AddressBook/AddressBook.h>
#import "LoginController.h"
#define RongCloudToken   @"0vpO/qj78bNaEqnlz4020jxiW6opAUIMSv0R3OxJV1MSDArJqoYtxWjhy98m3vdvK6K+OAG9OUZAdTpG5bsmSA=="
//
#define RongCloudAPPKey  @"x18ywvqf8fktc"


@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
        UIFont *font = [UIFont fontWithName:@"Helvetica-Bold" size:18];
        NSDictionary *textAttributes = @{
                                         NSFontAttributeName : font,
                                         NSForegroundColorAttributeName : PQ_COLOR_YELLOW
                                         };
        [[UINavigationBar appearance] setTitleTextAttributes:textAttributes];
        [[UINavigationBar appearance] setTintColor:PQ_COLOR_YELLOW];
        [[UINavigationBar appearance]
         setBarTintColor:[UIColor blackColor]];

    //去除uitabbar边线
    [[UITabBar appearance] setShadowImage:[[UIImage alloc]init]];
    
    [[UITabBar appearance] setBackgroundImage:[[UIImage alloc]init]];
    //主窗口设置
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
//    PQTabBarController *PQtabBar = [[PQTabBarController alloc] init];
     LoginController *LoginVC =[[LoginController alloc] init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:LoginVC];
    
    
   
    self.window.rootViewController = nav;
    [self.window makeKeyAndVisible];
    [self IphoneAddressList];
    //注册融云
    [[RCIM sharedRCIM] initWithAppKey:RongCloudAPPKey];
    //连接融云服务器
    [[RCIM sharedRCIM] connectWithToken:RongCloudToken success:^(NSString *userId) {
       
    } error:^(RCConnectErrorCode status) {
       
    } tokenIncorrect:^{
        //token过期或者不正确。
        //如果设置了token有效期并且token过期，请重新请求您的服务器获取新的token
        //如果没有设置token有效期却提示token错误，请检查您客户端和服务器的appkey是否匹配，还有检查您获取token的流程。
        
    }];
    
    return YES;
}
//手机通讯录设定
-(void)IphoneAddressList{
    
    /**
     授权: iOS6开始,必须授权
     */
    
    //1. 获取授权状态
    ABAuthorizationStatus status = ABAddressBookGetAuthorizationStatus();
    
    //2. 判断没有授权就授权
    /**
     kABAuthorizationStatusNotDetermined = 0,    未决定
     kABAuthorizationStatusRestricted,          一些许可配置阻止了与通讯录的交互
     kABAuthorizationStatusDenied,               拒绝
     kABAuthorizationStatusAuthorized         授权
     */
    if (status == kABAuthorizationStatusNotDetermined) {
        
        //3. 获取系统通讯录
        ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
        
        //4. 授权
        //RequestAccess: 请求授权
        ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
            if (granted) {
                NSLog(@"授权成功");
            } else {
                NSLog(@"授权失败");
            }
        });
        
        //5. 释放CF对象
        CFRelease(addressBook);
        
    }
    
  
    
}


- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
